package com.ruitu.health_1.ui.fragment;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.AutoLoginModel;
import com.ruitu.health_1.mvp.presenter.AutoLoginPresenter;
import com.ruitu.health_1.ui.ActionActivity;
import com.ruitu.health_1.ui.CollectActivity;
import com.ruitu.health_1.ui.CouponListActivity;
import com.ruitu.health_1.ui.OrderListActivity;
import com.ruitu.health_1.ui.PersonalCenterActivity;
import com.ruitu.health_1.ui.ScanActivity;
import com.ruitu.health_1.ui.SettingActivity;
import com.ruitu.health_1.ui.WalletActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.user.User;
import com.ruitu.router_module.util.ReqUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.Context.SENSOR_SERVICE;

public class MineFragment extends BaseFragment<AutoLoginPresenter.AutoLogin, AutoLoginModel.AutoLoginM> implements SensorEventListener {
    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private RelativeLayout toolbar;
    private LinearLayout ll_login_or_data;
    private LinearLayout ll_comment;
    private ScrollView scrollView;
    private TextView tv_mine_title;
    private TextView tv_name;
    private TextView tv_step;
    private LinearLayout ll_collect;
    private LinearLayout ll_attention;
    private LinearLayout ll_balance;
    private LinearLayout ll_integral;
    private TextView tv_integral;
    private TextView tv_balance;
    private LinearLayout ll_discount_coupon;
    private ImageView iv_header;
    private ImageView iv_scan;
    private ImageView iv_action;
    private ImageView iv_qrcode;
    private ImageView iv_setting;
    private ImageView iv_integral;

    private LinearLayout ll_wait_pay;
    private LinearLayout ll_wait_send;
    private LinearLayout ll_is_send;
    private LinearLayout ll_is_over;

    private LinearLayout rl_all_order;//全部订单

    private SensorManager mSensorManager;//
    private Sensor stepCounter;
    private float mSteps = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Arad.bus.register(this);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 获取SensorManager管理器实例
        mSensorManager = (SensorManager) getActivity().getSystemService(SENSOR_SERVICE);
        // getSensorList用于列出设备支持的所有sensor列表
        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_STEP_COUNTER);
        stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        if (stepCounter != null) {
            mSensorManager.registerListener(this, stepCounter, 1000000);
        }
        autoLogin();
        View v = inflater.inflate(R.layout.fragment_mine, container, false);
        toolbar = v.findViewById(R.id.toolbar);
        ll_login_or_data = v.findViewById(R.id.ll_login_or_data);
        ll_comment = v.findViewById(R.id.ll_comment);
        scrollView = v.findViewById(R.id.scrollView);
        tv_mine_title = v.findViewById(R.id.tv_mine_title);
        tv_name = v.findViewById(R.id.tv_name);
        tv_step = v.findViewById(R.id.tv_step);
        iv_header = v.findViewById(R.id.iv_header);
        iv_scan = v.findViewById(R.id.iv_scan);
        iv_qrcode = v.findViewById(R.id.iv_qrcode);
        iv_setting = v.findViewById(R.id.iv_setting);
        iv_action = v.findViewById(R.id.iv_action);
        iv_integral = v.findViewById(R.id.iv_integral);
        ll_collect = v.findViewById(R.id.ll_collect);
        ll_attention = v.findViewById(R.id.ll_attention);
        tv_integral = v.findViewById(R.id.tv_integral);
        tv_balance = v.findViewById(R.id.tv_balance);
        ll_discount_coupon = v.findViewById(R.id.ll_discount_coupon);
        ll_wait_pay = v.findViewById(R.id.ll_wait_pay);
        ll_wait_send = v.findViewById(R.id.ll_wait_send);
        ll_is_send = v.findViewById(R.id.ll_is_send);
        ll_balance = v.findViewById(R.id.ll_balance);
        ll_integral = v.findViewById(R.id.ll_integral);
        ll_is_over = v.findViewById(R.id.ll_is_over);
        rl_all_order = v.findViewById(R.id.rl_all_order);
        setOnClickListener(ll_login_or_data, iv_scan, ll_collect, iv_action, tv_integral, tv_balance,
                ll_attention, iv_qrcode, ll_wait_pay, ll_wait_send, ll_is_send, ll_is_over, ll_discount_coupon,
                iv_setting, rl_all_order, ll_balance,ll_comment);
        toolbar.getBackground().setAlpha(0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if (scrollY >= ll_login_or_data.getHeight() / 2) {
                        if (CommonUtils.isLogin()) {
                            tv_mine_title.setText(AppHolder.instance().getUser().getUsername());
                        }
                        toolbar.getBackground().setAlpha(255);
                    } else {
                        toolbar.getBackground().setAlpha(0);
                        tv_mine_title.setText("");
                    }
                }
            });
        }

        return v;
    }

    private void autoLogin() {
        String token = Arad.preferences.getString("token");
        //自动登录
        if (!TextUtils.isEmpty(token)) {
            Map<String, String> map = new HashMap<>();
            map.put("token", token);
            p.autoLogin(ReqUtil.getBody(map, true));
        }
    }

    private Bundle bundle;

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (ll_login_or_data == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                startActivity(PersonalCenterActivity.class);
            }
        }
        if (iv_scan == view) {
            startActivity(ScanActivity.class);
        }
        if (iv_qrcode == view) {

        }
        if (ll_collect == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                startActivity(CollectActivity.class);
            }
        }
        if (ll_attention == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                startActivity(CollectActivity.class);
            }
        }

        if (tv_integral == view) {
            startActivity(ActionActivity.class);
        }

        if (ll_balance == view) {
            startActivity(WalletActivity.class);
        }

        if (ll_discount_coupon == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                startActivity(CouponListActivity.class);
            }
        }

        if (ll_wait_pay == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                bundle = new Bundle();
                bundle.putInt("status", 1);
                startActivity(OrderListActivity.class, bundle);
            }
        }

        if (ll_wait_send == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                bundle = new Bundle();
                bundle.putInt("status", 2);
                startActivity(OrderListActivity.class, bundle);
            }
        }
        if (ll_comment == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                bundle = new Bundle();
                bundle.putInt("status", 3);
                startActivity(OrderListActivity.class, bundle);
            }
        }
        if (ll_is_over == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                bundle = new Bundle();
                bundle.putInt("status", 4);
                startActivity(OrderListActivity.class, bundle);
            }
        }
        if (iv_setting == view) {
            startActivity(SettingActivity.class);
        }
        if (rl_all_order == view) {
            if (CommonUtils.isLoginToActivity(getActivity())) {
                bundle = new Bundle();
                bundle.putInt("status", 0);
                startActivity(OrderListActivity.class, bundle);
            }
        }
    }


    @Override
    public void onSensorChanged(SensorEvent event) {

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setLogin(EventModel eventModel) {
        if (eventModel.getEventCode() == 19110400) {
            User user = (User) eventModel.getEventObj();
            setUserData(user);
        }

        if (eventModel.getEventCode() == 19112000) {
            try {
                String step = (String) eventModel.getEventObj();
                tv_step.setText(step);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        if (eventModel.getEventCode() == 2020031000) {
            autoLogin();
        }
    }

    public void setUserData(User user) {
        AppHolder.instance().setUser(user);
        if (user != null) {
            tv_name.setText(user.getUsername());
            Glide.with(getContext()).load("http://" + user.getIcon()).apply(Arad.opts_circle).into(iv_header);
            Arad.preferences.putString("token", user.getToken()).flush();
            Arad.bus.post(new EventModel(19122300));
            tv_integral.setVisibility(View.VISIBLE);
            iv_integral.setVisibility(View.GONE);
            tv_integral.setText(user.getWallet().getSellerPoint());
        } else {
            tv_name.setText("登录");
            Glide.with(getContext()).load(R.mipmap.icon_default_head).into(iv_header);
            tv_integral.setVisibility(View.GONE);
            iv_integral.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Arad.bus.unregister(this);
    }
}
